﻿<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html lang="en">
<%@ include file="../admin/top.jsp"%>
<script src="${ctx}/resource/pageView.js" type="text/javascript"></script>
</head>
<body class="easyui-layout" style="font-family: '微软雅黑'">   
    <div data-options="region:'center',split:true,border:false,title:'角色列表'" id="role_list" onclick="$('body').layout('collapse','east');"></div>   
    <div data-options="region:'east',split:true,border:false,title:'权限列表',collapsed: true" style="width: 425px">
    	<div id="sys_menu_toolbar" style="padding:5px;height:auto">
		    <div>
		   
		    	<a href="#" class="easyui-linkbutton" iconCls="icon-save" plain="true" onclick="save();">保存授权</a>
		    	<span class="toolbar-item dialog-tool-separator"></span>
		    </div>
		</div>
    	<table id="sys_menu_treegrid"></table>
    </div>   
<script type="text/javascript">
$(function(){   
	$("#role_list").pageView({
		view:{
			pagination:true,
			showSearchToolbar:true,
			showButtonToolbar:true,
			searchToolbar:[{
				type:'text',
				label:"昵称",
				name:"name"
			},{
				type:'text',
				label:"电话",
				name:"phone"
			}],
			buttons:[{
				type:'add',
				label:"添加",
				iconCls:"icon-add",
				href:"${ctx}/page/system-role-role_add",
				action:"${ctx}/role/add",
				width:500,
				height:400
			},{
				type:'delete',
				label:"删除",
				iconCls:"icon-remove",
				src:"${ctx}/role/del"
			},{
				type:'edit',
				label:"修改",
				iconCls:"icon-edit",
				href:"${ctx}/page/system-role-role_add",
				action:"${ctx}/role/add",
				width:500,
				height:400
			}]
		},
		data:{
			url:"${ctx}/role/listPage",
			singleSelect:true,
			columns : [ [ {
				field : 'id',
				checkbox : true
			}, {
				field : 'code',
				title : '角色编码',
				sortable : true,
				width : 100
			}, {
				field : 'name',
				title : '角色名称',
				sortable : true,
				width : 100
			}, {
				field : 'remark',
				title : '描述',
				width : 200
			}]],
			onSelect:function(rowIndex, rowData){
				initMenus(rowData.roleId);
				var p = $("body").layout("panel", "east")[0].clientWidth;  
	            if (p<=0){
	            	$("body").layout("expand","east");
	            }  
				
			}
		}
	});

	$('#sys_menu_treegrid').treegrid({   
		method: "POST",
	    url:'${ctx}/menu/listTree', 
	    fit : true,
		fitColumns : true,
		border : false,
		idField : 'menuId',
		treeField:'menuName',
		parentField : 'parentId',
		iconCls: 'menuIcon',
		animate:true, 
		rownumbers:true,
		striped:true,
		singleSelect:false,
	    columns:[[    
			{field:'ck',checkbox:true,width:100},   
	        {field:'menuName',title:'名称',width:100}
	    ]],
	    onClickRow:function(row){  
           $(this).treegrid('cascadeCheck',{  id:row.menuId, deepCascade:true });  
        },
        toolbar:'#sys_menu_toolbar'
	});
});
var current_roleId = null;
function initMenus(roleId){
	current_roleId = roleId;
	$('#sys_menu_treegrid').treegrid('unselectAll');
	$.get("${ctx}/role/rights/getRights",{roleId:roleId},function(data){
		if(data){
			for(var i=0,j=data.length;i<j;i++){
				$('#sys_menu_treegrid').treegrid('select',data[i].menuId);
			}
		} 
	});
}

function save(){
	parent.$.messager.confirm('提示', '确认要保存修改？', function(data){
	if (data){
		var menuIdList=[];
		var data=$('#sys_menu_treegrid').treegrid('getSelections');
		for(var i=0,j=data.length;i<j;i++){
			menuIdList.push(data[i].menuId);
		}
		if(current_roleId==null) {
			parent.$.messager.show({ title : "提示",msg: "请选择角色！", position: "bottomRight" });
			return;
		}
		$.post("${ctx}/role/rights/save",{roleId:current_roleId,menuIds:menuIdList.join(",")},function(data){
			parent.$.messager.show({ title : "提示",msg: "操作成功", position: "bottomRight" });
		});
	} 
	});
}
</script>
</body>
</html>